#include<func.h>
int main()
{
    char* ip ="192.168.237.128";
    char* port =  "8080";

    int socket_fd= socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(socket_fd,-1,"socket");

    int ret;
    struct sockaddr_in sockaddr;
    sockaddr.sin_family=AF_INET;
    sockaddr.sin_port=htons(atoi(port));
    sockaddr.sin_addr.s_addr=inet_addr(ip);

    connect(socket_fd,(struct sockaddr*) &sockaddr,sizeof(sockaddr));

    int epfd = epoll_create(1);
    ERROR_CHECK(epfd,-1,"epoll_create");

    struct epoll_event event; 
    event.events= EPOLLIN;
    event.data.fd= STDIN_FILENO;   
    ret = epoll_ctl(epfd,EPOLL_CTL_ADD,STDIN_FILENO,&event);
    ERROR_CHECK(ret,-1,"epoll_ctl");

    event.data.fd=socket_fd;
    ret = epoll_ctl(epfd,EPOLL_CTL_ADD,socket_fd,&event);
    ERROR_CHECK(ret, -1, "epoll_ctl");
    int flag=0;



    while(1)
    {
        struct epoll_event eventarr[2];
        int ready_num = epoll_wait(epfd,eventarr,2,-1);
        ERROR_CHECK(ready_num,-1,"epoll_wait");



        char arr[60];
        for(int i=0;i<ready_num;i++)
        {
            if(eventarr[i].data.fd==STDIN_FILENO)
            {
                ret=read(STDIN_FILENO,arr,sizeof(arr));
                ERROR_CHECK(ret,-1,"READ");
                arr[ret-1]='\0';
                printf("%s\n",arr);
                ret -=1;
                printf("ret:%d\n",ret);
                send(socket_fd,&ret,sizeof(int),MSG_NOSIGNAL);


                send(socket_fd,arr,ret,MSG_NOSIGNAL);

            }

            else 
            {

                char a;
                recv(socket_fd,&a,1,MSG_WAITALL);
                if(a=='0')
                {
                    printf("no file\n");
                    continue;
                }
                else
                {
                    int filefd=open(arr,O_RDWR|O_CREAT|O_TRUNC,0666);
                    ERROR_CHECK(filefd,-1,"open");

                    off_t size;
                    recv(socket_fd,&size,sizeof(off_t),MSG_WAITALL);

                    char buf[1024]={0};
                    int all=0;
                    while((ret = recv(socket_fd,buf,sizeof(buf),MSG_DONTWAIT))>0)
                    {
                        off_t recvnum = write(filefd,buf,ret);
                        all += recvnum;
                        char sig[21];
                        int n=2*((double)all/size)*10;
                        memset(sig,'>',n);
                        for(int i=n;i<20;i++)
                            sig[i]=' ';
                        float see=((float)all)/size;
                        sig[20]='\0';
                        printf("%s%.2f%%\r",sig,see*100);
                    }
                    printf("\n done\n");
                    if(ret==0)
                    {
                        flag=1;
                        break;
                    }

                    close(filefd);
                }
            }
        }
        if(flag==1)
            break;
    }
    close(epfd);
    close(socket_fd);
}


